Android Gradle 7.0 使用maven

您所在的位置:网站首页 publish publicate Android Gradle 7.0 使用maven

Android Gradle 7.0 使用maven

2023-11-28 13:46| 来源: 网络整理| 查看: 265

环境

Android Studio Arctic Fox | 2020.3.1 Patch 2 Build #AI-203.7717.56.2031.7678000, built on August 27, 2021 Runtime version: 11.0.10+0-b96-7249189 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Linux 5.4.0-89-generic

问题1:http协议的仓库不安全

Gradle 7.0默认强制使用https的仓库地址,直接使用的话,会报错如下:

Could not resolve all dependencies for configuration ‘:classpath’. Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository ‘maven(http://...:8081/repository/maven-public/)’ to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See https://docs.gradle.org/7.0.2/dsl/org.gradle.api.artifacts.repositories.UrlArtifactRepository.html#org.gradle.api.artifacts.repositories.UrlArtifactRepository:allowInsecureProtocol for more details.

解决1:设置allowInsecureProtocol

通过设置 allowInsecureProtocol 为 true,来支持http的仓库地址。详情见官方说明:UrlArtifactRepository:allowInsecureProtocol

buildscript { repositories { maven { allowInsecureProtocol true url 'http://xxx.xx.xx.xx:8081/repository/maven-public/' } google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.0.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } task clean(type: Delete) { delete rootProject.buildDir } 问题2:私有仓库组件依赖不成功

远程进行依赖时,旧版本可以依赖成功的一个组件,报错:

Failed to resolve: com.xx.xx:utils:1.0.0

1,检查project 的 build.gradle,明明已经设置好了maven地址引用。 buildscript { repositories { maven { allowInsecureProtocol true url 'http://xxx.xx.xx.xx:8081/repository/maven-public/' credentials { username = NEXUS_USERNAME password = NEXUS_PASSWORD } } google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.0.2" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }

发现个问题,旧版本一般都有的allprojects {},不见了!!!,莫非是换了人间,经查询果然是!!!

2,找到“新人间”,根目录的settings.gradle

修改前

dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() jcenter() // Warning: this repository is going to shut down soon } } rootProject.name = "xxx" include ':demo' include ':Library'

修改后

dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { maven { allowInsecureProtocol true url 'http://xxx.xxx.xxx.xxx:8081/repository/maven-public/' } google() mavenCentral() } } rootProject.name = "xxx" include ':demo' include ':Library' 问题3:apply plugin: ‘maven’ 不再支持,原先的上传脚本废了。

原来的脚本

//打包发布 apply plugin: 'maven' //打包main目录下代码和资源的 task task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } uploadArchives { repositories.mavenDeployer { name = 'mavenCentralReleaseDeployer' repository(url: MAVEN_URL) { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } snapshotRepository(url: MAVEN_SNAPSHOT_URL) { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.version = "1.1.2-SNAPSHOT" pom.artifactId = "xxx" pom.groupId = GROUP_ID pom.name = "xxx" pom.packaging = 'aar' } } artifacts { archives androidSourcesJar }

新的Publish脚本,确实费了一番周折,参考了android官网 : Use the Maven Publish plugin 和 gradle官网 :Example 8. Publishing a Java library。

//打包发布 apply plugin: 'maven-publish' //打包main目录下代码和资源的 task task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } afterEvaluate { publishing { publications { // Creates a Maven publication called "release". release(MavenPublication) { // Applies the component for the release build variant. from components.release // You can then customize attributes of the publication as shown below. groupId = GROUP_ID artifactId = 'xxxx' version = '1.0.0' } // Creates a Maven publication called “debug”. debug(MavenPublication) { // Applies the component for the debug build variant. from components.debug groupId = GROUP_ID artifactId = 'xxx' version = '1.0.0-SNAPSHOT' } } repositories { maven { name = "nexus" allowInsecureProtocol true url MAVEN_GROUP_URL //nexus3没有开启匿名用户访问的话,添加认证信息 credentials { username = NEXUS_USERNAME password = NEXUS_PASSWORD } def releasesRepoUrl = MAVEN_URL def snapshotsRepoUrl = MAVEN_SNAPSHOT_URL url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl } } } }

至此,可以正常使用了。

呜谢

Maven Publish Plugin

publishing_maven

MavenArtifactRepository

maven-publish插件的使用

Android使用自己封装的maven-publish插件(maven 和 maven-publish),发布到自己或者公司的私有仓库流程

Android Studio Arctic Fox | 2020.3.1、Gradle 7.0升级记录

Android Studio Arctic Fox gradle 配置

Gradle 7.X Maven publish 适配

gradle7.0之后,android库发布至MavenCentral流程变化。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3